package com.hex.ds.hdrs.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.hex.ds.hdrs.app.converter.IAppExecStrategyConverter;
import com.hex.ds.hdrs.app.dao.AppExecStrategyDao;
import com.hex.ds.hdrs.app.dto.AppExecStrategyDto;
import com.hex.ds.hdrs.app.po.AppExecStrategyPo;
import com.hex.ds.hdrs.app.service.IAppExecStrategyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Package com.hex.ds.hdrs.app.service.impl
 * @ClassName AppExecStrategyServiceImpl
 * @Description 应用自定义策略业务实现
 * @Author yu liuyang
 * @Date 2023/4/16 10:25
 * @Version v1.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppExecStrategyServiceImpl implements IAppExecStrategyService {

    @Autowired
    private AppExecStrategyDao appExecStrategyDao;

    @Autowired
    private IAppExecStrategyConverter appExecStrategyConverter;

    /**
     * @Method queryListByAppCode <br>
     * @Param [appCode] <br>
     * @Return java.util.List<com.hex.ds.hdrs.app.po.AppExecStrategy> <br>
     * @Description 根据应用编码查询对应的自定义策略 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/16 10:28 <br>
     * @Version V1.0 <br>
     */
    public List<AppExecStrategyPo> queryListByAppCode(String appCode) {
        return appExecStrategyDao.queryListByAppCode(appCode);
    }

    /*
     * @Method: add <br>
     * @Param: [appExecStrategyDtoList, appCode] <br>
     * @Return: void <br>
     * @Description：新增应用自定义策略<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/19 19:25 <br>
     * @Version： V2.0.2<br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(List<AppExecStrategyDto> appExecStrategyDtoList, String appCode) {
        if (CollectionUtil.isNotEmpty(appExecStrategyDtoList)) {
            appExecStrategyDtoList.stream().forEach((appExecStrategyDto) -> {
                add(appExecStrategyDto, appCode);
            });
        }
    }

    /*
     * @Method: add <br>
     * @Param: [appExecStrategyDto] <br>
     * @Return: void <br>
     * @Description：新增应用自定义策略<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/19 19:25 <br>
     * @Version： V2.0.2<br>
     */
    private void add(AppExecStrategyDto appExecStrategyDto, String appCode) {
        AppExecStrategyPo appExecStrategyPo = appExecStrategyConverter.setDefaultForAdd(appExecStrategyDto, appCode);
        int count = appExecStrategyDao.insert(appExecStrategyPo);
        if (count != 1) {
            throw new RuntimeException("添加应用与策略关联失败！");
        }
    }

    /*
     * @Method: modifyByAppCode <br>
     * @Param: [appExecStrategyDtoList, appCode] <br>
     * @Return: void <br>
     * @Description：更新应用自定义策略<br>
     * @Author： wz.li<br>
     * @Date： 2023/4/19 19:25 <br>
     * @Version： V2.0.2<br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void modifyByAppCode(List<AppExecStrategyDto> appExecStrategyDtoList, String appCode) {
        deleteByAppCode(appCode);
        add(appExecStrategyDtoList, appCode);
    }

    /**
     * @Method deleteByAppCode <br>
     * @Param [appCode] <br>
     * @Return void <br>
     * @Description 删除应用自定义策略, 可能为空 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/16 10:29 <br>
     * @Version V1.0 <br>
     */
    public void deleteByAppCode(String appCode) {
        appExecStrategyDao.deleteByAppCode(appCode);
    }

}
